<?php
namespace Admin\Controller;


class ComponentController extends \Common\Controller\BaseController {

    private $_model;
    public function __construct()
    {
        parent::__construct();
        $this->_model = new \Admin\Model\CommentsModel();
    }

    public function index(){

        $this->display();
    }
    //personal diary个人日记
    public function personal_diary(){

        $list = $this->_model->getPersonalDiaryData();
        $this->assign('list',$list);
        $this->display();
    }
    //后台日记首页
    public function pdindex(){
        $list = $this->_model->getPersonalDiaryData();
        $this->assign('list',$list);
        $this->display();
    }
    //后台日记增加
    public function pdadd(){

        if(!empty($_POST)){
            $arr = array(
                'title'=>I('title'),
                'time'=>I('time'),
                'content'=>I('content')
            );
            $result = $this->_model->setPersonalDiaryData($arr);
            if($result){
                $this->success("添加日记成功",U('Component/pdindex'));
            }else{
                $this->error("添加失败");
            }
        }else{
            $this->display();
        }
    }
    //后台日记删除
    public function pddel(){
        $result = $this->_model->delPersonalDiaryData(I('id'));
        if($result){
            $this->success("删除成功",U('Component/pdindex'));
        }else{
            $this->error('删除失败');
        }
    }
    //后台日记修改
    public function pdupd(){
        $type = I('type');
        if($type == 1){
            $list = $this->_model->getPersonalDiaryData(I('id'));
            $this->assign('list',$list);
            $this->display();
        }else{
            $arr = array(
                'title'=>I('title'),
                'time'=>I('time'),
                'content'=>I('content'),
                'id'=>I('id')
            );
            $result = $this->_model->setPersonalDiaryData($arr,$arr['id']);
            if($result){
                $this->success('日记修改成功',U('Component/pdindex'));
            }else{
                $this->error('日记修改失败');
            }
        }

    }

    //留言板
    public function message_board(){

        if(!empty($_POST)){

            //echo "进入留言板增加函数了";
            //var_dump($_POST);

            $data = array(
                'parent'=>"无",
                'belong'=>0,
                'content'=>I('content'),
                'time'=>I('time'),
                'email'=>"无",
                'username'=>session('username')?session('username'):"游客",
                'type'=>1,
            );
			if($data['content']==''){
				echo 'llbnwk';
				return;
			}
            //新增留言数据
            if(!$this->_model->setMessageData($data)){
                echo "留言添加失败！";
                //$this->error("留言添加失败！");
            }
        }else{
            $obj = $this->_model->getMessageData();
            $this->assign('commentsdata',$obj);
            $this->display();
        }
    }
    //后台留言板首页
    public function mdindex(){

        $list = $this->_model->getMessageData();
        $this->assign('list',$list);
        $this->display();
    }
    //后台留言板增加
    public function mdadd(){

        $this->display();
    }
    //后台留言板删除
    public function mddel(){

        $result = $this->_model->delMessageBoardData(I('id'));
        if($result){
            $this->success("删除成功",U('Component/mdindex'));
        }else{
            $this->error('删除失败');
        }
    }
    //后台留言板修改
    public function mdupd(){

        $list = $this->_model->getMessageData(I('id'));
        if(I('type') == 1){
            $this->assign('list',$list);
            $this->display();
        }else{
            $arr = array(
                "id"=>I("id"),
                "parent"=>I('parent')?I('parent'):$list['parent'],
                "belong"=>I('belong')?I('belong'):$list['belong'],
                "content"=>I('content')?I('content'):$list['content'],
                "time"=>I('time')?I('time'):$list['time'],
                "email"=>I('email')?I('email'):$list['email'],
                "username"=>I('username')?I('username'):$list['username'],
                "type"=>1,
            );
            //var_dump($arr);die;
            $result = $this->_model->setMessageData($arr,$arr['id']);
            if($result){
                $this->success('修改留言成功',U('Component/mdindex'));
            }else{
                $this->error('修改留言失败');
            }
        }
    }


    //表白墙
    public function reward(){

        $list = $this->_model->getRewardData();
        $this->assign('list',$list);
        $this->display();
    }
    //表白墙后台
    public function rdindex(){

        $list = $this->_model->getRewardData();
        $this->assign('list',$list);
        $this->display();
    }
    //后台表白墙增加
    public function rdadd(){
        if(!empty($_POST)){
            $arr = array(
                'toname'=>I('toname'),
                'fromname'=>I('fromname'),
                'time'=>I('time'),
                'content'=>I('content')
            );

            $result = $this->_model->setRewardData($arr);
            if($result){
                $this->success('添加表白信息成功',U('reward'));
            }else{
                $this->error('添加表白信息失败');
            }
        }else{
            $this->display();
        }

    }
    //后台表白墙删除
    public function rddel(){

        $result = $this->_model->delRewardData(I('id'));
        if($result){
            $this->success('删除成功',U('Component/reward'));
        }else{
            $this->error('删除失败');
        }
    }
    //后台表白墙修改
    public function rdupd(){

        $this->display();
    }
}